$(function() {
	var appendOffice = "<tr><td class = 'officeService-id'></td><td rowspan = '1' class = 'office-name'><input class = 'second-blank-answer'  name='officeName'></td><td class = 'officeService-services'><input class = 'second-blank-answer' name='services'></td><td class = 'officeService-issupport'><select id='issupport' name='issupport' ><option value='-1' selected='selected'></option><option value='1'  ${officeService.issupport==1?'selected='selected'':''}>√</option><option value='0'  ${officeService.issupport==0?'selected='selected'':''}>×</option></select></td><td class = 'officeService-op'><button class = 'addOfficeServices'>添加</button><button class = 'delAllOfficeServices'>删除</button></td></tr>";
	var appendService = "<tr><td class = 'officeService-id'></td><td class = 'officeService-services'><input class = 'second-blank-answer'  name='services'></td><td class = 'officeService-issupport'><select id='issupport' name='issupport' ><option value='-1' selected='selected'></option><option value='1'  ${officeService.issupport==1?'selected='selected'':''}>√</option><option value='0'  ${officeService.issupport==0?'selected='selected'':''}>×</option></select></td><td class = 'officeService-op'><button class = 'addOfficeServices'>添加</button><button class = 'delOfficeServices'>删除</button></td></tr>" ;
	$("#addOffice").on("click", function() {
		$("#officeServiceTable").append(appendOffice);
		updateGridIndex();
	});

	// 追加业务表格
	$(".grid").on("click", ".addOfficeServices", function() {
		mergeCell($(this).parent());
		$(this).parent().parent().after(appendService);
		updateGridIndex();
	});

	// 删除当前业务表格
	$(".grid").on("click", ".delOfficeServices", function() {
		var officeCell = unMergeCell($(this).parent());
		var currentRow = $(this).parent().parent();
		$(currentRow).remove();
		updateGridIndex();
	});

	$(".grid").on("click", ".delAllOfficeServices", function() {
		var officeCell = unMergeCell($(this).parent());
		var currentRow = $(this).parent().parent();
		$(currentRow).addClass("del");
		currentRow = $(currentRow).next();
		while (true) {
			var tempOffice = $(currentRow).children(".office-name");
			if ($(tempOffice).length > 0 || $(currentRow).length < 1) {
				break;
			}
			$(currentRow).addClass("del");
			currentRow = $(currentRow).next();
		}
		$(".del").remove();
		updateGridIndex();
	});
	$(window).bind("scroll resize",function(e){
		var scrolltop=jQuery(window).scrollTop();
		scrolltop>=100?($("#dw").css("display","block")):($("#dw").css("display","none"));
	});
	$("#button2").click(function(){
		savaOrupdate();
  	});
    $("#button1").click(function(){
		savaOrupdate();
    });	
    
    $("select").on("change",function(){
		var isShare = $(this).attr("class");
		var isShareValue = $(this).val();
		var shareType = "shareType" + isShare.substring(7);
		if(isShareValue==1){
			$("."+shareType).css("display","inline");
		} else {
			$("."+shareType).css("display","none");
			$("."+shareType).find(":checkbox").prop("checked",false);
		}
	});
    
    $(".shareType").on("click","span",function(){
    	var isChecked = $(this).find(":checkbox").prop("checked");
    	$(this).find(":checkbox").prop("checked", !isChecked);
    });
    updateGridIndex();
});


function updateGridIndex(){
// var maxRows = $("#officeServiceTable tr").length - 1 ;
	$("#officeServiceTable td.officeService-id").each(function(i,item){
		$(this).html(i+1);
	});
}
function mergeCell(currentCell) {
	var officeCell = searchOfficeCell(currentCell);
	var temp = parseInt(officeCell.attr("rowspan") == null ? 1 : officeCell
			.attr("rowspan"));
	officeCell.attr("rowspan", temp + 1);
}

function unMergeCell(currentCell) {
	var officeCell = searchOfficeCell(currentCell);
	var temp = parseInt(officeCell.attr("rowspan") < 2 ? 1 : officeCell
			.attr("rowspan"));
	officeCell.attr("rowspan", temp - 1);
	return officeCell;
}

function searchOfficeCell(currentCell) {
	var currentRow = $(currentCell).parent();
	while (true) {
		var tempOffice = $(currentRow).children(".office-name");
		if ($(tempOffice).length > 0) {
			return tempOffice;
			break;
		}
		currentRow = $(currentRow).prev();
	}
}

function savaOrupdate(){
 	var rowspanValues = $("#officeServiceTable").find(".office-name").map(function() {
		  return $(this).attr("rowspan");
		}).get().join(',');
		
		var officeidValue = $("input[name=officeid]").map(function() {
		  return $(this).val();
		}).get().join(',');
		var officeNameValue = $("input[name=officeName]").map(function() {
		  return $(this).val();
		}).get().join(',');
		var officeServiceidValue = $("input[name=officeServiceid]").map(function() {
		  return $(this).val();
		}).get().join(',');
		var servicesValue = $("input[name=services]").map(function() {
		  return $(this).val();
		}).get().join(',');
		var issupportValue = $("select[name=issupport]").map(function() {
		  return $(this).val();
		}).get().join(',');
		var informationIdValue = $("input[name=informationId]").map(function() {
		  return $(this).val();
		}).get().join(',');
		var isShareValue = $("select[name=isShare]").map(function() {
		  return $(this).val();
		}).get().join(',');
		var shareTypeValue = $("div[name=shareType]").map(function() {
			var $shareTypeList = $(this).find(":checkbox")
			var temp = "'";
			$shareTypeList.each(function(i,item){
				if($(item).prop("checked"))
					temp = temp + $(item).val();
				else
					temp = temp + "" ;
				
				if(i != $shareTypeList.length -1 )
					temp += ",";
				else
					temp += "'";
			});
			return temp;
		}).get().join(';');
		$.post("com.future.servlet/DepartmentServlet?flag=2",{
			item0101:$("#item0101").val(),
			item0102:$("#item0102").val(),
			item0201:$("#item0201").val(),
			item0202:$("#item0202").val(),
			item0203:$("#item0203").val(),
			item0204:$("#item0204").val(),
			item0205:$("#item0205").val(),
			item0206:$("#item0206").val(),
			item0301:$("#item0301").val(),
			item0302:$("#item0302").val(),
			item0401:$("#item0401").val(),
			item0402:$("#item0402").val(),
			officeids:officeidValue,
			officeNames:officeNameValue,
			officeServiceids:officeServiceidValue,
			servicess:servicesValue,
			issupports:issupportValue,
			informationIds:informationIdValue,
			isShares:isShareValue,
			shareTypes:shareTypeValue,
			rowspans:rowspanValues
		},function(returnData){
			if(returnData){
				alert("提交成功！");
			}else{
				alert("提交失败！");
			}
	});
 }